/*
 * Created on 7-Nov-2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.mtdownload.download;


/**
 * Sorter
 * @author sergiu
 */
public class DownloadTasksSorter{
	/**
	 * Constructor argument values that indicate to sort items by 
	 * description, owner or percent complete.
	 */
	public final static int DESTINATION 		= 1;
	public final static int SPEED 				= 2;
	public final static int PERCENT_COMPLETE 	= 3;
	public final static int THREADS			 	= 4;

	private int criteria=1;
	
	
	public DownloadTasksSorter(int criteria){
		super();
		setCriteria(criteria);
	}
	
	

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.ViewerSorter#compare(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
	 */
	public int compare(Object e1, Object e2) {
		DownloadTask t1=(DownloadTask)e1;
		DownloadTask t2=(DownloadTask)e2;
		
		switch(criteria){
			case DESTINATION:
				return compareDestination(t1,t2);
			case SPEED:
				return compareSpeed(t1,t2);
			case PERCENT_COMPLETE:
				return comparePercentComplete(t1,t2);
			case THREADS:
				return compareThreads(t1,t2);
			default:
				return 0;
		}
	}

	/**
	 * Returns a number reflecting the collation order of the given tasks
	 * based on the number of threads
	 *
	 * @param task1
	 * @param task2
	 * @return a negative number if the first element is less  than the 
	 *  second element; the value <code>0</code> if the first element is
	 *  equal to the second element; and a positive number if the first
	 *  element is greater than the second element
	 */
	private int compareThreads(DownloadTask task1, DownloadTask task2) {
		int result = task1.getThreadNum() - task2.getThreadNum();
		result = result < 0 ? 1 : (result > 0) ? -1 : 0;  
		return result;
	}
	
	/**
	 * Returns a number reflecting the collation order of the given tasks
	 * based on the speed
	 *
	 * @param task1
	 * @param task2
	 * @return a negative number if the first element is less  than the 
	 *  second element; the value <code>0</code> if the first element is
	 *  equal to the second element; and a positive number if the first
	 *  element is greater than the second element
	 */
	private int compareSpeed(DownloadTask task1, DownloadTask task2) {
		long result = task1.getSpeed() - task2.getSpeed();
		result = result < 0 ? 1 : (result > 0) ? -1 : 0;  
		return (int)result;
	}
	
	/**
	 * Returns a number reflecting the collation order of the given tasks
	 * based on the percent completed.
	 *
	 * @param task1
	 * @param task2
	 * @return a negative number if the first element is less  than the 
	 *  second element; the value <code>0</code> if the first element is
	 *  equal to the second element; and a positive number if the first
	 *  element is greater than the second element
	 */
	private int comparePercentComplete(DownloadTask task1, DownloadTask task2) {
		double result = task1.getPercentComplete() - task2.getPercentComplete();
		result = result < 0 ? 1 : (result > 0) ? -1 : 0;  
		return (int)result;
	}

	/**
	 * Returns a number reflecting the collation order of the given tasks
	 * based on the destination.
	 *
	 * @param task1 the first task element to be ordered
	 * @param task2 the second task element to be ordered
	 * @return a negative number if the first element is less  than the 
	 *  second element; the value <code>0</code> if the first element is
	 *  equal to the second element; and a positive number if the first
	 *  element is greater than the second element
	 */
	protected int compareDestination(DownloadTask task1, DownloadTask task2) {
		return task1.getDestinationFileName().compareTo(task2.getDestinationFileName());
	}

	


	/**
	 * @return Returns the criteria.
	 */
	public int getCriteria() {
		return criteria;
	}
	/**
	 * @param criteria The criteria to set.
	 */
	private void setCriteria(int criteria) {
		this.criteria = criteria;
	}
}
